//
//  LatLongSelectionController.m
//  Tazama NGO
//
//  Created by jsheriff on 3/15/11.
//  Copyright 2011 The MITRE Corporation. All rights reserved.
//

#import "LatLongSelectionController.h"
#import "DraggableAnnotation.h"
#import "DraggableAnnotationView.h"

@implementation LatLongSelectionController

@synthesize delegate;
@synthesize location;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(IBAction)savePressed:(id)sender
{
    double latitude = [latitudeTextField.text doubleValue];
    double longitude = [longitudeTextField.text doubleValue];
	location = CLLocationCoordinate2DMake(latitude, longitude);
	if ([delegate respondsToSelector:@selector(didSaveItem)])
	{
		[delegate didSaveItem];
	}
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad {
	UIBarButtonItem *doneButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(savePressed:)] autorelease];
	self.navigationItem.rightBarButtonItem = doneButton;
	
	latitudeTextField.text = [NSString stringWithFormat:@"%2.4f", location.latitude];
    longitudeTextField.text = [NSString stringWithFormat:@"%3.4f", location.longitude];
    
    mapView.delegate = self;
	mapView.mapType = MKMapTypeHybrid;
    mapView.showsUserLocation = NO;

    MKCoordinateRegion region;
    region.center = location;
    region.span.latitudeDelta = .06;
    region.span.longitudeDelta = .06;
    [mapView setRegion:region animated:YES];
    
    DraggableAnnotation *annotation = [[DraggableAnnotation alloc] init];
    annotation.coordinate = location;
    [mapView addAnnotation:annotation];
    
    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (MKAnnotationView *)mapView:(MKMapView *)localMapView viewForAnnotation:(id <MKAnnotation>)annotation {
	if ([annotation isKindOfClass:[MKUserLocation class]]) {
        return nil;
	}
	else if ([annotation isKindOfClass:[DraggableAnnotation class]]) {
		DraggableAnnotationView *annotationView = (DraggableAnnotationView *)[localMapView dequeueReusableAnnotationViewWithIdentifier:@"DraggableAnnotationView"];
		if (annotationView == nil)
		{
			annotationView = [[[DraggableAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"DraggableAnnotationView"] autorelease];
		}
		
//		annotationView.annotation = annotation;
		UIImageView	 *foImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MagnifyingGlassMarker40.png"]];
        annotationView.image = foImageView.image;
//		[annotationView addSubview:foImageView];
        annotationView.draggable = YES;
//		annotationView.image = [UIImage imageNamed:@"Transparent33.png"];
//		annotationView.canShowCallout = YES;
//		[annotationView setRightCalloutAccessoryView:[UIButton buttonWithType:UIButtonTypeDetailDisclosure]];
		return annotationView;
	}
	else {
		return nil;
	}
    
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState {
    if (newState == MKAnnotationViewDragStateNone) {
        location = annotationView.annotation.coordinate;
        latitudeTextField.text = [NSString stringWithFormat:@"%2.4f", location.latitude];
        longitudeTextField.text = [NSString stringWithFormat:@"%2.4f", location.longitude];
    }
}

@end
